package classicalSATeaching;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class mainJFrame{
    static JFrame frame = new JFrame();// 创建一个窗口
    static JPanel panel = new JPanel();
    static JLabel labTitle = new JLabel("经典软件体系结构");//设置窗口标题
    static JTextArea teachText = new JTextArea();
    static JTextArea methodText = new JTextArea();
    static JComboBox<String> jcb = new JComboBox<String>();
    static JButton jb1 = new JButton("教学该体系结构");
    static JButton jb2;
    static JButton jb3 = new JButton("运行");
    static JLabel label =new JLabel();

    public static void start(){
        frame.setTitle("经典软件体系结构教学软件");// 设置窗口标题
        frame.setBounds(200, 100, 1600, 1000);// 设置窗口位置和大小
        frame.setBackground(Color.gray);

        frame.setLayout(null);

        Font font = new Font("宋体", Font.BOLD, 16);



        //大标题

        Font font1 = new Font("宋体", Font.BOLD, 30);//设置字形，字体和字号
        labTitle.setFont(font1);
        panel.add(labTitle);
        panel.setBounds(650, 120, 300, 80);//设置窗口位置和大小
        frame.add(panel);


        //文本教学内容

        teachText.setBounds(1000, 10, 580, 480);
        teachText.setLineWrap(true);        //激活自动换行功能
        teachText.setWrapStyleWord(true);  // 激活断行不断字功能
        teachText.setFont(font);
        frame.add(teachText);

        //关键函数方法

        methodText.setBounds(10, 300, 980, 620);
        methodText.setLineWrap(true);        //激活自动换行功能
        methodText.setWrapStyleWord(true);  // 激活断行不断字功能
        methodText.setFont(font);
        frame.add(methodText);

        //下拉框

        jcb.setBounds(1050,580,200,30);
        jcb.addItem("主程序-子程序");
        jcb.addItem("面向对象");
        jcb.addItem("事件系统");
        jcb.addItem("管道-过滤器");
        jcb.setFont(font);
        frame.add(jcb);

        //按钮
        if(fileInfo.fileChoosed==false)
            jb2= new JButton("选择txt文件");
        else
            jb2 = new JButton("已选择txt文件");


        jb1.setBounds(1300,550,180,120);
        jb2.setBounds(1050,800,180,80);
        jb3.setBounds(1300,750,180,150);
        jb1.setFont(font);
        jb2.setFont(font);
        jb3.setFont(font);
        teachingAction a1=new teachingAction();
        fileChooseAction a2=new fileChooseAction();
        runAction a3=new runAction();


        jb1.addActionListener(a1);
        jb2.addActionListener(a2);
        jb3.addActionListener(a3);
        frame.add(jb1);
        frame.add(jb2);
        frame.add(jb3);



        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        frame.setVisible(true);// 显示窗口
    }


    //返回该窗口
    public static void back(){
        if(fileInfo.fileChoosed==false)
            jb2.setText ("选择txt文件");
        else
            jb2.setText("已选择txt文件");

        //切换标题

        labTitle.setText(SAInfo.name);

        //图片
        //获取图片地址
        ImageIcon imageIcon = new ImageIcon(SAInfo.imageFileName);//命名不要重复
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        label.setBounds(-70,-70,800,450);
        frame.add(label);

        //教学文本
        teachText.setText(ioUtils.txtInput(SAInfo.txtFileName));

        //主要函数
        methodText.setText("关键代码如下：\n"+ ioUtils.txtInput(SAInfo.methodFileName));



        frame.setVisible(true);
    }

}


class  teachingAction implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
//        mainJFrame.jcb.getSelectedItem();
        new SAInfo((String)mainJFrame.jcb.getSelectedItem());
//        System.out.println(1);
        mainJFrame.back();
        mainJFrame.back();
    }
}

class  fileChooseAction implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        fileChooserJframe.start();

    }
}

class  runAction implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(3);

        if("主程序-子程序".equals(SAInfo.name)){
            kwic1.Main.main(fileInfo.fileName);
        }else if("面向对象".equals(SAInfo.name)){
            kwic2.Main.main(fileInfo.fileName);
        }else if("事件系统".equals(SAInfo.name)){
            kwic3.Main.main(fileInfo.fileName);
        }else if("管道-过滤器".equals(SAInfo.name)){
            try {
                kwic4.Main.main(fileInfo.fileName);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        runningResultJframe.start();
    }
}